import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { successResponse } from '@/lib/response';

// POST /api/client/friendly-links/list - 获取启用的友情链接列表
async function handler(req: NextRequest) {
  const { limit } = (await req.json().catch(() => ({ limit: 0 }))) as { limit?: number };

  const items = await prisma.friendlyLink.findMany({
    where: { isActive: 1 },
    orderBy: [
      { order: 'asc' },
      { createdAt: 'desc' },
    ],
    take: limit && limit > 0 ? limit : undefined,
  });

  const data = items.map((item) => ({
    ...item,
    createdAt: item.createdAt.toISOString(),
    updatedAt: item.updatedAt.toISOString(),
  }));

  return successResponse(data);
}

export const POST = withMiddleware(handler);